package com.ruoyi.project.system.controller;

import java.util.List;
import java.util.Set;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginBody;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.SysLoginService;
import com.ruoyi.framework.security.service.SysPermissionService;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysMenu;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysMenuService;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class SysLoginController {
  @Autowired
  private SysLoginService loginService;

  @Autowired
  private ISysMenuService menuService;

  @Autowired
  private SysPermissionService permissionService;

  @Autowired
  private TokenService tokenService;

  @Autowired
  private SysUserMapper userMapper;

  /**
   * 登录方法
   *
   * @param loginBody 登录信息
   * @return 结果
   */
  @PostMapping("/uniApp/login")
  public AjaxResult uniAppLogin(@RequestBody LoginBody loginBody) {
    String uuidString = UUID.randomUUID().toString().replace("-", "");
    loginBody.setUuid(uuidString);
    AjaxResult ajax = AjaxResult.success();
    // 生成令牌
    String token = loginService.uniAppLogin(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
        loginBody.getUuid());
    ajax.put(Constants.TOKEN, token);
    if (token != null) {
      SysUser sysUser = userMapper.selectOne(new QueryWrapper<SysUser>().eq("user_name", loginBody.getUsername()));
      ajax.put("userInfo",sysUser);
    }
    return ajax;
  }

  /**
   * 登录方法
   *
   * @param loginBody 登录信息
   * @return 结果
   */
  @PostMapping("/login")
  public AjaxResult login(@RequestBody LoginBody loginBody) {
    AjaxResult ajax = AjaxResult.success();
    // 生成令牌
    String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
        loginBody.getUuid());
    ajax.put(Constants.TOKEN, token);
    return ajax;
  }

  /**
   * 获取用户信息
   *
   * @return 用户信息
   */
  @GetMapping("getInfo")
  public AjaxResult getInfo() {
    LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
    SysUser user = loginUser.getUser();
    // 角色集合
    Set<String> roles = permissionService.getRolePermission(user);
    // 权限集合
    Set<String> permissions = permissionService.getMenuPermission(user);
    AjaxResult ajax = AjaxResult.success();
    ajax.put("user", user);
    ajax.put("roles", roles);
    ajax.put("permissions", permissions);
    return ajax;
  }

  /**
   * 获取路由信息
   *
   * @return 路由信息
   */
  @GetMapping("getRouters")
  public AjaxResult getRouters() {
    LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
    // 用户信息
    SysUser user = loginUser.getUser();
    List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
    return AjaxResult.success(menuService.buildMenus(menus));
  }
}
